package org.bonorsoft.modules.control.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.control.entity.CheckPhoto;
import org.bonorsoft.modules.control.service.ICheckPhotoService;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;

 /**
 * @Description: 检查图片关联表
 * @Author: WilliamYe
 * @Date:   2021-04-13
 * @Version: V1.0
 */
@Slf4j
@Controller
@RequestMapping("/control/checkPhoto")
public class CheckPhotoController extends BonorController<CheckPhoto, ICheckPhotoService> {
    private String prefix = "control/checkPhoto";

	@Autowired
	private ICheckPhotoService checkPhotoService;

	/**
	*首页
	*/
	@RequiresPermissions("control:checkPhoto:view")
    @GetMapping()
    public String index()
    {
     return prefix + "/checkPhoto";
    }

	/**
	 * 分页列表查询
	 *
	 * @param checkPhoto
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "检查图片关联表-分页列表查询")
	@RequiresPermissions("control:checkPhoto:list")
	@GetMapping(value = "/list")
	@ResponseBody
	public Result<?> queryPageList(CheckPhoto checkPhoto,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CheckPhoto> queryWrapper = QueryGenerator.initQueryWrapper(checkPhoto, req.getParameterMap());
		Page<CheckPhoto> page = new Page<CheckPhoto>(pageNo, pageSize);
		IPage<CheckPhoto> pageList = checkPhotoService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

    /**
    * 添加页面
    *
    */
    @GetMapping("/add")
    public String add()
    {
     return prefix + "/add";
    }

	/**
	 * 添加
	 *
	 * @param checkPhoto
	 * @return
	 */
	@AutoLog(value = "检查图片关联表-添加")
	@RequiresPermissions("control:checkPhoto:add")
	@PostMapping(value = "/add")
    @ResponseBody
	public Result<?> add(@Validated CheckPhoto checkPhoto) {
		checkPhotoService.save(checkPhoto);
		return Result.ok("图片添加成功！");
	}

    /**
    * 编辑页面
    */
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap map)
    {
     map.put("editInfo", checkPhotoService.getById(id));
     return prefix + "/edit";
    }

	/**
	 * 编辑
	 *
	 * @param checkPhoto
	 * @return
	 */
	@AutoLog(value = "检查图片关联表-编辑")
	@RequiresPermissions("control:checkPhoto:edit")
	@PostMapping(value = "/edit")
	@ResponseBody
	public Result<?> edit(@Validated CheckPhoto checkPhoto) {
		checkPhotoService.updateById(checkPhoto);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "检查图片关联表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		checkPhotoService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "检查图片关联表-批量删除")
	@RequiresPermissions("control:checkPhoto:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	@ResponseBody
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.checkPhotoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "检查图片关联表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		CheckPhoto checkPhoto = checkPhotoService.getById(id);
		return Result.ok(checkPhoto);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param checkPhoto
    */
    @AutoLog(value = "检查图片关联表-导出excel")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CheckPhoto checkPhoto) {
      return super.exportXls(request, checkPhoto, CheckPhoto.class, "检查图片关联表");
    }

    /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @AutoLog(value = "检查图片关联表-通过excel导入数据")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, CheckPhoto.class);
    }

    /**
     * 模板下载
     * @return
     */
    @AutoLog(value = "检查图片关联表-模板下载", operateType = 6)
    @RequestMapping(value = "/importTemplate")
    public ModelAndView importTemplate() {
        return super.importTemplateXls(CheckPhoto.class ,"检查图片关联表");
    }

}
